package demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import demo.common.Result;
import demo.entity.Stick;
import demo.mapper.SchoolMapper;
import demo.mapper.StickMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/forum")
public class StickController {
  @Resource
  StickMapper stickMapper;

  @PostMapping
  public Result<?> save(@RequestBody Stick stick){
    stickMapper.insert(stick);
    return Result.success();
  }
  @GetMapping
  public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            @RequestParam(defaultValue = "") String search) {
    LambdaQueryWrapper<Stick> wrapper = Wrappers.<Stick>lambdaQuery();
    if (StrUtil.isNotBlank(search)) {
      wrapper.like(Stick::getTitle, search);
    }
    Page<Stick> userPage = stickMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    return Result.success(userPage);
  }
}
